/*submit profile function*/
var submit_profile = function () {
	var email 	  = $.trim($('input[name=email]').val()),
		signature = $.trim($('input[name=signature]').val()),
		realname  = $.trim($('input[name=realname]').val()),
		gender    = $.trim($('input[name=gender]:checked').val()),
		mobile    = $.trim($('input[name=mobile]').val()),
		age       = $.trim($('input[name=age]').val()),
		$profile  = $('#profile');

	// Change form on loading state
	$profile.addClass('loading');

	$.myAjax(
		_CTX_PATH_ + '/setting/profile', 
		{
			email	  : email,
			signature : signature,
			realname  : realname,
			gender	  : gender,
			mobile	  : mobile,
			age 	  : age
		}, function (data) {
			// Remove loading state
			$profile.removeClass('loading');
			if (data.result == true) {
				alert('个人资料修改成功');
			} else {
				$profile.form('add errors', data.error).addClass('error');
			}
		}
	);
}

/**
 * Modify password function
 * @return none
 */
var submit_password = function () {
	var $currentPassword = $('input[name=currentPassword]'),
		$newPassword 	 = $('input[name=newPassword]'),
		$confirmPassword = $('input[name=confirmPassword]'),
		currentPassword  = hex_md5($.trim($currentPassword.val())),
		newPassword      = hex_md5($.trim($newPassword.val())),
		confirmPassword  = hex_md5($.trim($confirmPassword.val())),
		$password        = $('#password');

		// Change form on loading state
		$password.addClass('loading');

		$.myAjax(
			_CTX_PATH_ + '/setting/password',
			{
				currentPassword : currentPassword,
				newPassword 	: newPassword,
				confirmPassword : confirmPassword
			}, function (data) {
				$password.removeClass('loading');
				if (data.result == true) {
					alert('密码修改成功');
					$currentPassword.val("");
					$newPassword.val("");
					$confirmPassword.val("");
				} else {
					$password.form('add errors', data.error).addClass('error');
				}
			}
		);
}

var refresh_avatar = function (src) {
	$('.current_avatar').attr("src", src);
}

var setting = function() {
	/*Profile*/
	$('#profile').form({
		email: {
			identifier: 'email',
			rules: [
				{
					type: 'empty',
					prompt: '邮箱不能为空'
				},
				{
					type: 'email',
					prompt: '您输入的邮箱格式不正确'
				}
			]
		},
		signature: {
			identifier: 'signature',
			rules: [
				{
					type: 'maxLength[100]',
					prompt: '签名太长，小于100个字'
				}
			]	
		},
		realname: {
			identifier: 'realname',
			rules: [
				{
					type: 'maxLength[30]',
					prompt: '真实姓名太长，小于30个字'
				}
			]
		},
		mobile: {
			identifier: 'mobile',
			rules: [
			    {
			    	type: 'isNumeric',
			    	prompt: '输入的电话号码不正确'
			    }
			]
		},
		age: {
			identifier: 'age',
			rules: [
			    {
			    	type: 'isNumeric',
			    	prompt: '输入的年龄不是数字'
			    }
			]
		}
	}, {
		onSuccess: submit_profile
	});

	/*Password*/
	$('#password').form({
		currentPassword: {
			identifier: 'currentPassword',
			rules: [
				{
					type: 'empty',
					prompt: '当前密码不能为空'
				}
			]
		},
		newPassword: {
			identifier: 'newPassword',
			rules: [
				{
					type: 'empty',
					prompt: '新密码不能为空'
				},
				{
					type: 'length[5]',
					prompt: '密码长度至少为5位'
				}
			]	
		},
		confirmPassword: {
			identifier: 'confirmPassword',
			rules: [
				{
					type: 'empty',
					prompt: '新密码确认不能为空'
				},
				{
					type: 'match[newPassword]',
					prompt: '两次密码输入不一致'
				}
			]	
		}
	}, {
		onSuccess: submit_password
	});
	
	$('.ui.radio.checkbox').checkbox();
	
	$('input[name=file]').on('change', function () {
		$('#avatar').submit();
	});
}

$(document).ready(setting);